package com.hs.zhou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.bai.pojo.*;
import com.hs.bai.service.*;
import com.hs.ding.pojo.Dispatch;
import com.hs.ding.pojo.Transport;
import com.hs.ding.pojo.Vehicle;
import com.hs.ding.service.IDispatchService;
import com.hs.ding.service.ITransportService;
import com.hs.ding.service.IVehicleService;
import com.hs.hu.pojo.Receipt;
import com.hs.hu.service.IReceiptService;
import com.hs.lin.pojo.ExceptionHandling;
import com.hs.lin.pojo.ExceptionRegistration;
import com.hs.lin.service.IExceptionHandlingService;
import com.hs.lin.service.IExceptionRegistrationService;
import com.hs.zhou.pojo.Bill;
import com.hs.zhou.mapper.BillMapper;
import com.hs.zhou.pojo.BillVo;
import com.hs.zhou.service.IBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Xudongzhou
 * @since 2022-06-14
 */
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements IBillService {

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private IOrderbasicinfoService orderbasicinfoService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IGoodsinfoService goodsinfoService;

    @Autowired
    private IExpenseinfoService expenseinfoService;

    @Autowired
    private IBillService billService;

    @Autowired
    private IReceiptService receiptService;

    @Autowired
    private IVehicleService vehicleService;

    @Autowired
    private ITransportService transportService;

    @Autowired
    private IDispatchService dispatchService;

    @Autowired
    private IAddrinfoService addrinfoService;

    @Autowired
    private IExceptionRegistrationService exceptionRegistrationService;

    @Autowired
    private IExceptionHandlingService exceptionHandlingService;


    @Override
    public List<Bill> findAll(String consumerName) {


        QueryWrapper<Bill> qrname = new QueryWrapper<>();

        qrname.eq("customer_name",consumerName);

        List<Bill> bills = billService.list(qrname);



        for (Bill bill:bills){

            //查询订单
            QueryWrapper<Order> qr = new QueryWrapper<>();
            qr.eq("id",bill.getOrderid());
            Order order = orderService.getOne(qr);
            bill.setOrder(order);


            //查询费用信息
            Expenseinfo expenseinfo = expenseinfoService.getById(order.getTFId());
            bill.setExpenseinfo(expenseinfo);

        }

        System.out.println(bills);
        return bills;
    }

    @Override
    public Bill findById(String orderNumber) {

        QueryWrapper<Bill> qrb = new QueryWrapper<>();
        qrb.eq("orderid",orderNumber);
        Bill bill = billService.getOne(qrb);

        //查询订单
        QueryWrapper<Order> qr = new QueryWrapper<>();
        qr.eq("id",bill.getOrderid());
        Order order = orderService.getOne(qr);
        bill.setOrder(order);

        //根据order查询费用信息
        Expenseinfo expenseinfo = expenseinfoService.getById(order.getTFId());
        bill.setExpenseinfo(expenseinfo);

        //根据order查询订单地址信息
        Addrinfo addrinfo = addrinfoService.getById(order.getTAId());
        bill.setAddrinfo(addrinfo);

        //查询该订单所有货物信息
        QueryWrapper<Goodsinfo> qrgood = new QueryWrapper<>();

        qrgood.eq("order_id",bill.getOrderid());

        List<Goodsinfo> goodsinfos = goodsinfoService.list(qrgood);

        bill.setGoodsinfos(goodsinfos);

        //查询订单基本信息
        QueryWrapper<Orderbasicinfo> qrob = new QueryWrapper<>();
        qrob.eq("id",bill.getOrderid());
        Orderbasicinfo orderbasicinfo = orderbasicinfoService.getOne(qrob);
        bill.setOrderbasicinfo(orderbasicinfo);

        //查询订单异常信息
        QueryWrapper<ExceptionRegistration> qrer = new QueryWrapper<>();
        qrer.eq("o_id",bill.getOrderid());
        ExceptionRegistration exceptionRegistration = exceptionRegistrationService.getOne(qrer);

        if(!ObjectUtils.isEmpty(exceptionRegistration)){
            bill.setExceptionRegistration(exceptionRegistration);
            QueryWrapper<ExceptionHandling> qrh = new QueryWrapper<>();
            qrh.eq("ex_id",exceptionRegistration.getId());
            ExceptionHandling handling = exceptionHandlingService.getOne(qrh);
            bill.setExceptionHandling(handling);
        }else {
            ExceptionRegistration exceptionRegistration1 = new ExceptionRegistration();
            exceptionRegistration1.setId("无");
            exceptionRegistration1.setCreatedTime(null);
            bill.setExceptionRegistration(exceptionRegistration1);
            ExceptionHandling exceptionHandling = new ExceptionHandling();
            exceptionHandling.setCommitmentAmount("无");
            bill.setExceptionHandling(exceptionHandling);
        }
        //查询接收时间
        QueryWrapper<Receipt> qr1= new QueryWrapper<>();
        qr1.eq("r_order_id",bill.getOrderid());
        Receipt receipt = receiptService.getOne(qr1);
        bill.setReceipt(receipt);

        //查询总重量
        Float totalWeight = totalWeight(bill.getOrderid());
        bill.setTotalWeight(totalWeight);

        //查询总体积
        Float totalVolum = totalVolum(bill.getOrderid());
        bill.setTotalVolum(totalVolum);



        //查询派车单号
        String dispatch = searchDispatch(bill.getOrderid());
        bill.setDispatchNo(dispatch);

        //查询承运商
        String transport = searchTransport(bill.getOrderid());
        bill.setCarrierName(transport);


        System.out.println(bill);
        return bill;
    }

    //根据订单id查询总重量
    private Float totalWeight(String orderId){

        QueryWrapper<Goodsinfo> qr = new QueryWrapper<>();

        qr.eq("order_id",orderId);

        List<Goodsinfo> goods = goodsinfoService.list(qr);

        System.out.println(goods);

        float sum = 0;
        for (Goodsinfo good:goods){
            sum += good.getUnitWeight()*good.getNumber();
        }
        return sum;
    }

    //根据订单id查询总体积
    private Float totalVolum(String orderId){
        QueryWrapper<Goodsinfo> qr = new QueryWrapper<>();

        qr.eq("order_id",orderId);

        List<Goodsinfo> goods = goodsinfoService.list(qr);

        float sum=0;
        for(Goodsinfo good:goods){
            sum += good.getHigh()*good.getLength()*good.getWide()*good.getNumber();
        }
        return sum;
    }

    private String searchDispatch(String orderId){

        QueryWrapper<Vehicle> qr = new QueryWrapper<>();

        qr.eq("order_id",orderId);

        Vehicle vehicle = vehicleService.getOne(qr);

        if(!ObjectUtils.isEmpty(vehicle)){
            QueryWrapper<Transport> qrt = new QueryWrapper<>();

            qrt.eq("v_order_id",vehicle.getvOrderId());

            Transport transport = transportService.getOne(qrt);

            if(!ObjectUtils.isEmpty(transport)){

                QueryWrapper<Dispatch> qrd = new QueryWrapper<>();

                qrd.eq("t_order_id",transport.gettOrderId());

                Dispatch dispatch = dispatchService.getOne(qrd);

                if(!ObjectUtils.isEmpty(dispatch)){
                    return dispatch.getdOrderId();
                }
            }
        }
        return null;
    }

    private String searchTransport(String orderId){


        QueryWrapper<Vehicle> qr = new QueryWrapper<>();

        qr.eq("order_id",orderId);

        Vehicle vehicle = vehicleService.getOne(qr);

        if(!ObjectUtils.isEmpty(vehicle)){
            QueryWrapper<Transport> qrt = new QueryWrapper<>();

            qrt.eq("v_order_id",vehicle.getvOrderId());

            Transport transport = transportService.getOne(qrt);

            if(!ObjectUtils.isEmpty(transport)){
                return transport.getCarrierName();
            }

        }
        return null;
    }

    @Override
    public List<Bill> selectPageBill(Page<Bill> page,QueryWrapper<Bill> queryWrapper){
        return this.baseMapper.selectPageBill(page,queryWrapper);
    }

    @Override
    public PageInfo<Bill> queryParamPage(Integer currentPage,Integer pageSize,String orderId,String comsuName ,Integer billStatus){
        orderId = StringUtils.hasText(orderId)?"'"+orderId+"'":"'"+"%"+"'";
        comsuName = comsuName == null || "".equals(comsuName)?"'"+"%"+"'":"'"+comsuName+"'";
        //未提交 0   已提交1 全部2
        String bl = null;
        if(billStatus == 0){ bl = billStatus.toString();}

        if(billStatus == 1){ bl = billStatus.toString();}

        if(billStatus == 2){ bl = null;}

        PageHelper.startPage(currentPage,pageSize);
        List<Bill> bills = billMapper.selectParamPage(orderId, comsuName,bl);
        PageInfo<Bill> info = new PageInfo<>(bills);
        return info;

    }

}
